package top.lingchen.blogapi.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lingchen.blogapi.aop.LogAnnotation;
import top.lingchen.blogapi.cache.Cache;
import top.lingchen.blogapi.service.ArticleService;
import top.lingchen.blogapi.vo.Result;
import top.lingchen.blogapi.vo.params.ArticleParam;
import top.lingchen.blogapi.vo.params.PageParams;

/**
 * @Author 凌宸
 * @create 2021-12-15 下午 8:56
 * @Description
 * @Version 1.0
 */
@Slf4j
@RestController // json 数据进行交互
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    ArticleService articleService;


    /**
     * 首页 分页查询文章列表 /articles
     * @param params 包含 页码和每页显示输了
     * @return
     */
    @PostMapping
    @LogAnnotation(module = "文章", operator = "获取文章列表")
    @Cache(expire = 5 * 60 * 1000, name = "listArticle")
    public Result listArticle(@RequestBody PageParams params){
//        log.info("ArticleController ....");
//        int i = 1 / 0;
        return articleService.listArticle(params);
    }

    /**
     * 首页 最热文章 /articles/hot
     * @return
     */
    @PostMapping("/hot")
    @Cache(expire = 5 * 60 * 1000, name = "hot_article")
    public Result hotArticles(){
        int limit = 5;
        return articleService.hotArticles(limit);
    }

    /**
     * 首页 最热文章 /articles/new
     * @return
     */
    @PostMapping("/new")
    @Cache(expire = 5 * 60 * 1000, name = "new_article")
    public Result newArticles(){
        int limit = 5;
        return articleService.newArticles(limit);
    }

    /**
     * 首页 文章归档 /articles/listArchives
     * @return
     */
    @PostMapping("/listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }

    /**
     * 文章内容 /articles/view/{id}
     */
    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    /**
     * 发布文章服务 /articles/publish
     * @param articleParam
     * @return
     */
    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }
}
